package com.bjgzy.rating.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.mvp.contract.RatingDetailContract;
import com.bjgzy.rating.mvp.presenter.RatingDetailPresenter;
import com.bjgzy.rating.mvp.ui.fragment.EntriesFragment;
import com.bjgzy.rating.mvp.ui.fragment.RatingDetailFragment;
import com.bjgzy.rating.mvp.ui.fragment.WinningWorkFragment;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RatingDetailData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.alpha.QMUIAlphaButton;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-7 16:51:51
 */
@Route(path = RouterHub.RATING_RATINGDETAILACTIVITY)
public class RatingDetailActivity extends BaseActivity<RatingDetailPresenter> implements RatingDetailContract.View {
    public static final String MY_WORKS = "我的作品";
    public static final String TAKE_PART = "我要参加";
    @BindView(R2.id.tabs)
    TabLayout tabs;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;
    @BindView(R2.id.btn_submit)
    QMUIAlphaButton btnSubmit;
    @Autowired(name = Constants.ROUTER_DATA)
    RatingHomeData data;

    List<Fragment> mFragments;

    String[] mTitles = new String[]{
            "活动详情", "参赛作品", "获奖作品"
    };

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_rating_detail");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.rating_activity_detail;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mFragments = new ArrayList<>();
        RatingDetailFragment instance = RatingDetailFragment.newInstance(data);
        mFragments.add(instance);
        EntriesFragment listFragment = EntriesFragment.newInstance(data);
        mFragments.add(listFragment);
        WinningWorkFragment winningWorkFragment = WinningWorkFragment.newInstance(data);
        mFragments.add(winningWorkFragment);
        // 第二步：为ViewPager设置适配器
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getSupportFragmentManager(), Arrays.asList(mTitles), mFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(mTitles.length);
        // 第三步：将ViewPager与TableLayout 绑定在一起
        tabs.setupWithViewPager(viewpager);
        if (data.getUserRight().getCouldUpload() == 1) {
            btnSubmit.setVisibility(View.VISIBLE);
        } else {
            btnSubmit.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(RatingDetailData data) {
    }

    @OnClick(R2.id.btn_submit)
    public void onViewClicked() {
        if (btnSubmit.getText().toString().equals(MY_WORKS)) {
            Utils.customNavigation(RouterHub.RATING_MYWORKSACTIVITY, Constants.MY_WORKS, data);
        } else if (btnSubmit.getText().toString().equals(TAKE_PART)) {
            if (data.getResearchWorksItem().getFileType() == Constants.FILETYPE.DOCUMENT || data.getResearchWorksItem().getFileType() == Constants.FILETYPE.COMMON) {
                showMessage("手机端不支持参加此类型活动");
            } else {
                Utils.customNavigation(RouterHub.RATING_SIGNUP1ACTIVITY, Constants.CHOOSEGROUP, data);
            }
        }
    }

    @Subscriber(tag = RouterHub.RATING_RATINGDETAILACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.UPDATE_USER_WORKS_COUNT:
                if (msg.arg1 > 0) {
                    btnSubmit.setText(MY_WORKS);
                    btnSubmit.setVisibility(View.VISIBLE);
                } else {
                    btnSubmit.setText(TAKE_PART);
                }
                break;
            case EventBusTags.WORKS_UPLOAD_SUCCESS:
                killMyself();
                break;
            default:
        }
    }
}
